<?php
if (!defined('ZZQSS')){
	die("Access Denied");
}
/**
 * Referralsclass 邀请返利操作类
 * add()
 * set()
 * check()
 * editstate()
 * getdata()
 *
 */
class Referralsclass {
	var $db;
	var $table;
	var $tablepre;
	var $refmoney;
	function __construct(){
		//设置全局变量
		global $db,$tablepre,$cfg_referrals_money;
		$this->db=$db;
		$this->tablepre=$tablepre;
		$this->table=$tablepre."accountreferrals";
		$this->refmoney=$cfg_referrals_money;//返利额度 通过系统参数设置
	}
	function Referralsclass() {
		$this->__construct();
	}
	/**
	 * 添加返利单到数据库
	 * 执行前必须执行过set（）方法才行
	 * 确保refauth loginauto cookie正确才会执行
	 *@param gid int
	 * @param gname gtitle string
	 * @return true/false
	 */
	function add($gid,$gname){
		$timestamp=time();
		//获取cookie里面保存的信息
		$refauth=get_cookie('refauth');$loginauth=get_cookie('loginauth');
		if(!empty($refauth) && !empty($loginauth)) {
			list($ruid, $runame,$cookietime) = explode("\t", cookie_authcode($refauth,'DECODE'));
			list($uid, $uname,$pwd) = explode("\t", cookie_authcode($loginauth,'DECODE'));
		} else {
			$ruid = $runame =$cookietime= '';
			return false;
		}
		//判断获取数据正确性
		if(!empty($ruid) && !empty($uid) && !empty($runame) && !empty($uname) && !empty($cookietime) && ($cookietime > $timestamp)&&($ruid != $uid)){
			//放入数据库返利单子
			$this->db->query("insert into {$this->table}(uid,uname,recomenduid,recomenduname,gid,gound_name,money,addtime,state) VALUES({$uid},'{$uname}','{$ruid}','{$runame}','$gid','$gname','{$this->refmoney}',$timestamp,0)");						
			return true;
		}return false;
		
	}
	/**
	 * 设置邀请人信息保存到cookie里面
	 *
	 * @param string $runame
	 * @param int $ctime
	 * @return true/false
	 */
	function set($runame,$ctime=""){
		global $cfg_referrals_date;
		if(empty($ctime))$ctime=$cfg_referrals_date;
		$cookietime=time()+(3600*$ctime);
		if(!empty($runame)){
			$row = $this->db->fetch_first("Select * From {$this->tablepre}users where uname = '{$runame}'");
			if(is_array($row)){
				$refauth=$row['uid']."\t".$runame."\t".$cookietime;
				set_cookie('refauth',cookie_authcode($refauth,'ENCODE'),$cookietime);
				return true;
			}return false;
		}return false;	
	}
	/**
	 * 验证通过返利给用户
	 *
	 * @param unknown_type $aid
	 * @return true/false
	 */
	function check($aid){
		if(is_numeric($aid)){
			$row = $this->db->fetch_first("Select * From `$this->table` where aid = '{$aid}'");
			if(is_array($row)){
				include(ROOT_PATH."/includes/member.class.php");
				$m=new memberclass();
				$note="邀请返利";
				if($m->moneyedit($row['recomenduname'],$row['money'],$note)){
					$this->editstate($aid,1);//设置返利单状态为已成功返利
					unset($m);
					return true;
				}else return false;
			}else return false;
		}else return false;
	}
	/**
	 * 编辑返利单子状态 
	 * 状态0 默认 1正常返利 2 设置失效
	 *
	 * @param unknown_type $aid
	 * @param unknown_type $state
	 * @return true/false
	 */
	function editstate($aid,$state){
		if(is_numeric($aid)){
			$this->db->query("update `$this->table` SET state = '{$state}' WHERE aid='{$aid}'");
			return true;
		}else return false;	
	}
	/**
	 * 删除指定ID邀请返利单子
	 *
	 * @param unknown_type $aid
	 * @return unknown
	 */
	function del($aid){
		if(is_numeric($aid)){
			$this->db->query("delete from {$this->table} where aid='{$aid}'");
			return true;
		}return false;	
	}
	/**
	 * 获取数据数组
	 *
	 * @param string $limit
	 * @param string $where
	 * @param string $orderby
	 * @return array
	 */
	function getdata($limit="",$where="",$orderby="")
	{
		$tempdata=array();
		if(!empty($limit))$limit=" limit $limit ";
		if(!empty($where))$where=" where $where ";
		if(!empty($orderby))$orderby=" order by $orderby ";else $orderby=" order by aid desc";

		$sql="select * from {$this->table}{$where}{$orderby}{$limit}";
		$query =$this->db->query($sql);
		while($value = $this->db->fetch_array($query)) {
			//此处可进行数据整理
			$tempdata[]=$value;
		}
		return $tempdata;
	}
	function getcount($where=""){
		if(!empty($where))$where=" where $where";
		$count= $this->db->result_first("Select count(*) From `$this->table` $where");
		return $count;
	
	}
	
}
?>